iT邦幫忙

2024 iThome 鐵人賽

DAY 5
0
佛心分享-刷題不只是刷題

C/C++ 刷題30天系列 第 5

Day05__C語言刷LeetCode

  • 分享至 

  • xImage
  •  

嘗試這個禮拜開始每天寫兩題LeetCode~

1290. Convert Binary Number in a Linked List to Integer

tags: Easy、Linked List

Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list.
he most significant bit is at the head of the linked list.

解法1:

#include <math.h>
int getDecimalValue(struct ListNode* head) {
    int decimal = 1;
    int total = 0;
    int length = 0;
    struct ListNode *current = head;
    struct ListNode *temp = head;
    while (temp->next != NULL) {
        length += 1;
        temp = temp->next;
    }
    decimal = pow(2, length);

    while (current != NULL) {
        total += current->val * decimal;
        decimal = decimal / 2;
        current = current->next;
    }
    return total;
}

解法2:

位移操作在某些情況下可能比乘法操作更高效,特別是在需要進行大量計算時。兩者在邏輯上是等價的,但位移操作更接近底層硬件運算,可能更快。因此,使用 << 操作符的寫法會較有效率。

#include <math.h>
int getDecimalValue(struct ListNode* head) {
    int sum = 0;
    while(head != NULL){
        sum = (sum << 1) + head->val; 
        head = head->next;
    }
    return sum;
}

160. Intersection of Two Linked Lists

tags: Easy、Linked List

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
For example, the following two linked lists begin to intersect at node c1:
https://assets.leetcode.com/uploads/2021/03/05/160_statement.png
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
listA - The first linked list.
listB - The second linked list.
skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

解法:

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if (headA == NULL || headB == NULL) {
        return NULL;
    }
    struct ListNode *a = headA;
    struct ListNode *b = headB;

    while (a != b) {
        a = (a == NULL) ? headB : a->next;
        b = (b == NULL) ? headA : b->next;
    }
    return a;
}

上一篇
Day04__C語言刷LeetCode
下一篇
Day06__C語言刷LeetCode
系列文
C/C++ 刷題30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言